//
//  YTDSegmentedCell.swift
//  XingCeHelper
//
//  Created by 申铭 on 2022/12/9.
//

import UIKit

enum StatisticsYTD: String {
    case daily = "日"
    case weekly = "本周"
    case month = "本月"
    case all = "全部"
}

let kYTDSegmentedCell: String = "YTDSegmentedCell"

class YTDSegmentedCell: BaseTableViewCell {

    @IBOutlet weak var segmentedControl: UISegmentedControl!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        selectionStyle = .none
        backgroundColor = .clear
        
        segmentedControl.removeAllSegments()
        segmentedControl.insertSegment(withTitle: StatisticsYTD.daily.rawValue, at: 0, animated: true)
        segmentedControl.insertSegment(withTitle: StatisticsYTD.weekly.rawValue, at: 1, animated: true)
        segmentedControl.insertSegment(withTitle: StatisticsYTD.month.rawValue, at: 2, animated: true)
        segmentedControl.insertSegment(withTitle: StatisticsYTD.all.rawValue, at: 3, animated: true)
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.addTarget(self, action: #selector(segmentedControlChange(_ :)), for: .valueChanged)
    }

    @objc private func segmentedControlChange(_ segmented: UISegmentedControl) {
        let index = segmented.selectedSegmentIndex
        if index == 0 {
            Global.shared.currentStatisticsYTD = .daily
        } else if index == 1 {
            Global.shared.currentStatisticsYTD = .weekly
        } else if index == 2 {
            Global.shared.currentStatisticsYTD = .month
        } else if index == 3 {
            Global.shared.currentStatisticsYTD = .all
        }
        
        NotificationCenter.default.post(name: .refreshPieChart, object: nil)
    }
    
}
